<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="商品名" prop="cmName">
        <el-input v-model="dataForm.cmName" placeholder="商品名"></el-input>
      </el-form-item>
      <el-form-item label="商品logo" prop="cmLogo">
        <singleUpload v-model="dataForm.cmLogo"></singleUpload>
      </el-form-item>
      <el-form-item label="商品的价格" prop="cmPrice">
        <el-input
          v-model="dataForm.cmPrice"
          placeholder="商品的价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="介绍表的id" prop="cmInId">
        <el-input v-model="dataForm.cmInId" placeholder="介绍表的id"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="cmAmount">
        <el-input v-model="dataForm.cmAmount" placeholder="商品数量"></el-input>
      </el-form-item>
      <el-form-item label="商品已购数量" prop="cmAmounted">
        <el-input
          v-model="dataForm.cmAmounted"
          placeholder="商品已购数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="每用户限购数量" prop="cmQuota">
        <el-input
          v-model="dataForm.cmQuota"
          placeholder="每用户限购数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品分类的id" prop="stId">
        <!--   <el-input v-model="dataForm.stId" placeholder="商品分类的id"></el-input> -->
        <el-cascader
          v-model="ids"
          :options="options"
          :props="props"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="商家id" prop="mtId">
        <!--       <el-input v-model="dataForm.mtId" placeholder="商家id"></el-input>
 -->
        <el-cascader
          v-model="sjId"
          :options="sjone"
          :props="sjprops"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import singleUpload from "@/components/upload/singleUpload";
export default {
  components: { singleUpload } /* 注册进来 */,
  data() {
    return {
      props: {
        value: "stId",
        label: "stName",
        children: "childs",
      },
      sjprops: {
        value: "mtId",
        label: "mtName",
      },
      ids: [],
      sjId: [],
      sjone: [],
      options: [],
      visible: false,
      dataForm: {
        cmId: 0,
        cmName: "",
        cmLogo: "",
        cmPrice: "",
        cmInId: "",
        cmAmount: "",
        cmAmounted: "",
        cmQuota: "",
        stId: 0,
        mtId: 0,
      },
      dataRule: {
        cmName: [
          { required: true, message: "商品名不能为空", trigger: "blur" },
        ],
        cmLogo: [
          { required: true, message: "商品logo不能为空", trigger: "blur" },
        ],
        cmPrice: [
          { required: true, message: "商品的价格不能为空", trigger: "blur" },
        ],
        cmInId: [
          { required: true, message: "介绍表的id不能为空", trigger: "blur" },
        ],
        cmAmount: [
          { required: true, message: "商品数量不能为空", trigger: "blur" },
        ],
        cmAmounted: [
          { required: true, message: "商品已购数量不能为空", trigger: "blur" },
        ],
        cmQuota: [
          {
            required: true,
            message: "每用户限购数量不能为空",
            trigger: "blur",
          },
        ],
        stId: [
          { required: true, message: "商品分类的id不能为空", trigger: "blur" },
        ],
        mtId: [{ required: true, message: "商家id不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    //获取分类
    getSort() {
       this.options=[];
      this.$http({
        url: this.$http.adornUrl(`/mallgn/sort/list/tree`),
        method: "post",
        data: this.$http.adornData({}),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.options = data.data;
        }
      });
    },
    //获取商家
    getSJSort() {
       this.sjone=[];
       var page=1;
      this.$http({
        url: this.$http.adornUrl(`/mallgn/merchant/list?page=${page}`),
        method: "get",
        data: this.$http.adornParams({}),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.sjone = data.page.list;
        }
      });
    },

    init(id) {
    this.getSort();
    this.getSJSort();
      this.dataForm.cmId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.cmId) {
          this.$http({
            url: this.$http.adornUrl(
              `/mallgn/commodity/info/${this.dataForm.cmId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.cmName = data.commodity.cmName;
              this.dataForm.cmLogo = data.commodity.cmLogo;
              this.dataForm.cmPrice = data.commodity.cmPrice;
              this.dataForm.cmInId = data.commodity.cmInId;
              this.dataForm.cmAmount = data.commodity.cmAmount;
              this.dataForm.cmAmounted = data.commodity.cmAmounted;
              this.dataForm.cmQuota = data.commodity.cmQuota;
              this.dataForm.stId = data.commodity.stId;
              this.dataForm.mtId = data.commodity.mtId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/mallgn/commodity/${!this.dataForm.cmId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              cmId: this.dataForm.cmId || undefined,
              cmName: this.dataForm.cmName,
              cmLogo: this.dataForm.cmLogo,
              cmPrice: this.dataForm.cmPrice,
              cmInId: this.dataForm.cmInId,
              cmAmount: this.dataForm.cmAmount,
              cmAmounted: this.dataForm.cmAmounted,
              cmQuota: this.dataForm.cmQuota,
              stId:  this.ids[this.ids.length-1],
              mtId:  this.sjId[this.sjId.length-1],
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
  created() {
    this.getSort();
    this.getSJSort();
  },
};
</script>
